package gov.pr.celepar.sentinela.admin.action;

import java.util.Collection;

import gov.pr.celepar.sentinela.admin.dao.IconeFuncaoDAO;
import gov.pr.celepar.sentinela.admin.dao.factory.DAOFactoryServer;
import gov.pr.celepar.sentinela.admin.excecao.SentinelaServerException;
import gov.pr.celepar.sentinela.admin.form.PesquisaIconeForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class PesquisaIconeAction extends BaseAction {

	public String executar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.forward = "lista_icone";
		try {
			PesquisaIconeForm iconeForm = (PesquisaIconeForm) form;
			this.listarSistemasAutorizados(request);

			if (iconeForm.getCodSistema() != null
					&& !iconeForm.getCodSistema().equals("")) {
				DAOFactoryServer daof = DAOFactoryServer.getDAOFactory();
				IconeFuncaoDAO bi = daof.getIconeFuncaoDAO();
				Collection dados = bi.listar(Long.decode(iconeForm
						.getCodSistema()));

				if (dados != null && !dados.isEmpty()) {
					request.setAttribute("listaIcones", dados);
				} else {
					throw new SentinelaServerException(
							"Nenhum registro encontrado! ");
				}
			}
		} catch (SentinelaServerException appEx) {
			throw appEx;
		} catch (Exception e) {
			throw e;
		}
		return "lista_icone";
	}
}
